Cart = function() {
	this.data = [];
	this.total = 0;
};

Cart.prototype = {
	displayProd:function(price, product_id, short_name, qty) {
		var elem = $('<div id="elem_' + product_id + '" class="element"><div class="el_name">' + short_name + '</div><div id="price_' + product_id + '" class="el_price">' + price + '</div><div id="qty_' + product_id + '" class="el_qty"><input type="text" id="qty_' + product_id + '" class="qty" value="' + qty + '" /></div><div><input type="button" value="x" onclick="delete(\'elem_' + product_id + '\')" class="el_del"></div></div>');
		$("#cart").append(elem);
	},
	addProd:function() {
		var is = false;
		for (var i = 0; i < arguments.length; i++) {
			if (this.data.length > 0)
				for (var j = 0; j < this.data.length; j++) {
					if (this.data[j].product_id === arguments[i].product_id) {
						this.data[j].qty++;
						this.data[j].price += arguments[i].price;
						is = true;
						return;
					}
				}
			if (!is) {
				this.data.push(arguments[i]);
				is = false;
			}

			//this.incrementProd(this.data[i].product_id);

			this.displayProd(arguments[i].price, arguments[i].product_id, arguments[i].short_name, arguments[i].qty);
			//this.total(arguments[i].price);
		}
	},
	remProd:function() {
	},
	incrementProd:function(prod_id) {
		$('#qty_' + prod_id).value = $('#qty_' + prod_id).value + 1;
	},
	total:function(price) {
		this.total += price;
	}
};
Cart.prototype.constructor = Cart;

$(function() {
	var myCart = new Cart();
	//myCart.addProd({"price":"10", "product_id":"1", "shortName":"beer", "qty":"1"});
	//myCart.addProd({"price":"10", "product_id":"1", "shortName":"beer", "qty":"1"});
});


fffCommerce.api.Storer = function(objectName, data) {
	this.objectName = objectName;
	this.data = data;
	this.store = function() {
		jQuery.ajax({"type": "POST",
			url: "http://192.168.182.145:8080/" + this.objectName + "/",
			data: JSON.stringify(this.data),
			success: function(result) {
				this.orderPostResultHandler(result);
			}.bind(this)
		});
	}
	this.orderPostResultHandler = function(data) {
		alert(data);
	}
};

Function.prototype.bind = function(object) {
	var self = this;
	var params = Array.prototype.slice(arguments, 1)
	return function() {
		return self.apply(object, params.concat(arguments));
	};
}

/*
 var orderStorer = new fffCommerce.api.Storer('Order',{"order_datails": "sdfdsf"});
 orderStorer.store();

 */